package com.k.activemq.openwire4;

import com.k.activemq.openwire3.PubSubUtil;
import com.k.activemq.openwire4.domain.ConnData;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.AsyncCallback;

import javax.jms.*;

public class Pub4 {
    public static void main(String[] args) {
        System.out.println("mq main start");
        // conn data
        String url = ConnData.url;
        String username = ConnData.username;
        String password = ConnData.password;
        String topic = ConnData.publishTopic;
        Boolean pubSubDomain = ConnData.pubSubDomain;
        Boolean persistentData = ConnData.PubConfig.persistentData;

        // conn
        ActiveMQConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;
        try {
            // ConnectionFactory
            connectionFactory = new ActiveMQConnectionFactory(url);
            // Connection
            connection = connectionFactory.createConnection(username, password);
            connection.start();
            // Session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            // 目的地
            Destination destination = pubSubDomain ? session.createTopic(topic) : session.createQueue(topic);

            // 发送者
            producer = session.createProducer(destination);
            // 数据是否持久化(需要Broker 配置持久化 KaHaDB Mysql等 无特殊配置默认会使用 KaHaDB)
            if(persistentData){
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            }
            // 发送者 要发送的消息
            Message message11 = PubSubUtil.createMessage(session, "{\"header\":{\"system_id\":\"\",\"subsystem_id\":\"\",\"msgid\":null,\"related_id\":null,\"send_time\":\"2023-09-21T11:08:55.8816136Z\",\"cmd\":\"Out_Status\",\"request\":\"PUC_SdsStatus\",\"request_type\":\"1\",\"reponse\":null,\"reponse_type\":null,\"action\":null},\"body\":{\"RemoteFilePath\":null,\"RemoteFilePath_27SP2\":null,\"FileName\":null,\"FileType\":null,\"FileSize\":null,\"TimeSpan\":null,\"Authgrant\":null,\"SapType\":null,\"Recipient\":{\"ID\":\"00001-924-data4puc6953\",\"Number\":\"data4puc6953\",\"Numbertype\":\"7\",\"PucId\":\"00001\",\"SystemId\":\"924\"},\"Sdscontent\":\"12345\",\"SDSType\":0,\"Sender\":{\"ID\":\"812A6DEA-55B2-416F-AB5C-083A2660AC57\",\"Number\":\"102\",\"Numbertype\":\"0\",\"PucId\":\"00001\",\"SystemId\":\"924\"},\"ProductName\":null}}", true);
            Message message22 = PubSubUtil.createMessage(session, "{\"header\":{\"system_id\":\"\",\"subsystem_id\":\"\",\"msgid\":null,\"related_id\":null,\"send_time\":\"2023-09-21T11:08:55.8816136Z\",\"cmd\":\"Out_Status\",\"request\":\"PUC_SdsStatus\",\"request_type\":\"1\",\"reponse\":null,\"reponse_type\":null,\"action\":null},\"body\":{\"RemoteFilePath\":null,\"RemoteFilePath_27SP2\":null,\"FileName\":null,\"FileType\":null,\"FileSize\":null,\"TimeSpan\":null,\"Authgrant\":null,\"SapType\":null,\"Recipient\":{\"ID\":\"00001-924-data4puc6953\",\"Number\":\"data4puc6953\",\"Numbertype\":\"7\",\"PucId\":\"00001\",\"SystemId\":\"924\"},\"Sdscontent\":\"32768\",\"SDSType\":0,\"Sender\":{\"ID\":\"00001-924-102\",\"Number\":\"102\",\"Numbertype\":\"0\",\"PucId\":\"00001\",\"SystemId\":\"924\"},\"ProductName\":null}}", true);
            Message message33 = PubSubUtil.createMessage(session, "{\"header\":{\"system_id\":\"\",\"subsystem_id\":\"\",\"msgid\":null,\"related_id\":null,\"send_time\":\"2023-09-21T11:08:55.8816136Z\",\"cmd\":\"Out_Status\",\"request\":\"PUC_SdsStatus\",\"request_type\":\"1\",\"reponse\":null,\"reponse_type\":null,\"action\":null},\"body\":{\"RemoteFilePath\":null,\"RemoteFilePath_27SP2\":null,\"FileName\":null,\"FileType\":null,\"FileSize\":null,\"TimeSpan\":null,\"Authgrant\":null,\"SapType\":null,\"Recipient\":{\"ID\":\"00001-924-data4puc6953\",\"Number\":\"data4puc6953\",\"Numbertype\":\"7\",\"PucId\":\"00001\",\"SystemId\":\"924\"},\"Sdscontent\":\"32768\",\"SDSType\":0,\"Sender\":{\"ID\":\"00001-924-102\",\"Number\":\"102\",\"Numbertype\":\"0\",\"PucId\":\"00001\",\"SystemId\":\"924\"},\"ProductName\":null}}", true);
            // 发送者-发送消息
            producer.send(message11);
//            producer.send(message22);
//            producer.send(message33);
            // 发送 Broker 回执, 需要开启事务，数据持久化。通常是队列
            /*
             session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
             Destination destination = session.createQueue("queueName")
             ActiveMQMessageProducer producer = session.createProducer(destination);
             producer.setDeliveryMode(DeliveryMode.PERSISTENT);
             producer.send(message, new AsyncCallback() {
                @Override
                public void onSuccess() {
                    System.out.println("Message sent successfully");
                }

                @Override
                public void onException(JMSException e) {
                    System.out.println("Error sending message");
                    e.printStackTrace();
                }
            });
            session.commit();
             */
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            PubSubUtil.close(producer, null, session, connection);
        }
        System.out.println("mq main finish");
    }
}
